/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-19 15:33:33
 * @LastEditTime: 2022-07-10 16:33:13
 * @LastEditors: liutq
 * @Reference:
 */
package user

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/service"
	"github.com/maxyang107/jinglan/utils"
)

/**
 * @description: 方法描述：获取邀请列表
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetInviteList(c *gin.Context) {
	var inviteParam common.AnnouncementList

	if err := c.BindQuery(&inviteParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")

	inviteParam.UserId = utils.InterfaceToUser(userid)
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	uil := service.UserService.GetInviteList(inviteParam)
	common.ResponseListToJson(http.StatusOK, uil.Page, uil.PerPage, uil.Total, uil.Data, c)
}

/**
 * @description: 方法描述：获取藏品列表
 * @Author: maxyang
 * @return {*}
 * @param {common.AnnouncementList} query_
 * 当🆔不为空的时候返回对应商品id的列表
 */
func GetUserCollectList(c *gin.Context) {
	var collectParam common.PramCollect

	if err := c.BindQuery(&collectParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	collectParam.UserId = utils.InterfaceToUser(userid)

	cl := service.UserService.GetUserCollectList(collectParam)
	common.ResponseListToJson(http.StatusOK, cl.Page, cl.PerPage, cl.Total, cl.Data, c)
}

/**
 * @description: 方法描述：开盲盒
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func OpenBlindBox(c *gin.Context) {
	var param common.Detail
	if err := c.ShouldBind(&param); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")

	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	param.UserId = utils.InterfaceToUser(userid)

	g, err := service.UserService.OpenBlindBox(param)
	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}

	common.ResponseDetailToJson(http.StatusOK, g, "succexx", c)
}

/**
 * @description: 方法描述：获取藏品详情
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetUserCollectById(c *gin.Context) {
	var collectParam common.Detail

	if err := c.BindQuery(&collectParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	collectParam.UserId = utils.InterfaceToUser(userid)

	mcd := service.UserService.GetUserCollectDetail(collectParam)

	if mcd.OwnerUid == 0 && mcd.GoodsId == 0 {
		common.ResponseToJson(http.StatusOK, common.Fail, "没有找到藏品信息", c)
		return
	}

	common.ResponseDetailToJson(http.StatusOK, mcd, "success", c)
}

/**
 * @description: 方法描述：邀请排行榜
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetInviteRankList(c *gin.Context) {
	var inviteParam common.AnnouncementList
	if err := c.BindQuery(&inviteParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")

	inviteParam.UserId = utils.InterfaceToUser(userid)
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}
	ril := service.UserService.GetInviteRankList(inviteParam)

	common.ResponseListToJson(http.StatusOK, ril.Page, ril.PerPage, ril.Total, ril.Data, c)
}

func GetUserInfo(c *gin.Context) {
	userid, exists := c.Get("UserId")

	userId := utils.InterfaceToUser(userid)
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	u := service.UserService.GetUserInfo(userId)
	u.Password = ""
	u.Salt = ""
	common.ResponseDetailToJson(http.StatusOK, u, "success", c)
}
